package programDetails;

import letcode.util.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 从单链表中循环赋值取值
 * @Date: 2020/4/14
 * @Author: 许群星
 */
public class _2_1_SingleLinkedList {
    public static void main(String[] args) {
        //取值，使用循环
        ListNode l1=new ListNode(7);
        l1.next=new ListNode(2);
        l1.next.next=new ListNode(4);
        l1.next.next.next=new ListNode(3);

        List<Integer> list=new ArrayList<>();
        while (l1!=null){
            System.out.println(l1.val);
            list.add(l1.val);
            l1=l1.next;
        }

        System.out.println("-----------------------");
        //赋值    倒序赋值
        List<Integer> test=new ArrayList<>();
        test.add(1);
        test.add(2);
        test.add(3);
        test.add(4);
        ListNode head=null;
        for (int i = 0; i < test.size(); i++) {
            ListNode temp=new ListNode(test.get(i));
            temp.next=head;
            head=temp;
        }
        System.out.println(head.val);
        System.out.println(head.next.val);
        System.out.println(head.next.next.val);
        System.out.println(head.next.next.next.val);
    }
}
